package com.xjoyt.pay.master.services;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xjoyt.pay.abstracts.AbstractService;
import com.xjoyt.pay.commons.ObjectUtils;
import com.xjoyt.pay.commons.PayTypeUtils;
import com.xjoyt.pay.dto.ExecuteResult;
import com.xjoyt.pay.dto.PageParam;
import com.xjoyt.pay.dto.PageResult;
import com.xjoyt.pay.master.po.Distributor;
import com.xjoyt.pay.master.po.DistributorCashRate;
import com.xjoyt.pay.master.po.DistributorRate;
import com.xjoyt.pay.master.repository.DistributorCashRateRepository;
import com.xjoyt.pay.master.repository.DistributorRateRepository;
import com.xjoyt.pay.master.repository.DistributorRepository;
import com.xjoyt.pay.master.vo.DistributorRateMap;
import com.xjoyt.pay.master.vo.RateEntry;
import com.xjoyt.pay.merchant.register.params.DistributorPageParam;
import com.xjoyt.pay.vo.PayType;

@Service
public class DistributorRateService extends AbstractService {

	@Autowired
	private DistributorRepository distributorRepository;

	@Autowired
	private DistributorRateRepository repository;

	@Autowired
	private DistributorCashRateRepository cashRepository;

	public PageResult<DistributorRateMap> loadPage(PageParam<DistributorPageParam> param) {
		PageResult<Distributor> page = distributorRepository.loadPage(param);
		List<DistributorRateMap> rows = new ArrayList<>();

		List<String> ids = new ArrayList<>();
		page.forEach(item -> {
			ids.add(item.id);
			DistributorRateMap vo = new DistributorRateMap();
			rows.add(vo);
			ObjectUtils.fieldCopy(item, vo);
		});

		List<DistributorRate> list = CollectionUtils.isEmpty(ids) ? new ArrayList<>()
				: repository.findAllByDistributorIdIn(ids);
		List<DistributorCashRate> cashList = CollectionUtils.isEmpty(ids) ? new ArrayList<>()
				: cashRepository.findAllByDistributorIdIn(ids);
		rows.forEach(item -> {
			item.rateList = new ArrayList<>();
			for (PayType type : PayType.values()) {
				RateEntry entry = new RateEntry();
				entry.type = type;
				item.rateList.add(entry);
				entry.name = PayTypeUtils.getName(type);
				for (DistributorRate distributorRate : list) {
					if (distributorRate.money == null || distributorRate.limit == null
							|| distributorRate.minimum == null)
						continue;

					if (type.equals(distributorRate.type) && item.id.equals(distributorRate.distributor.id)) {
						entry.rate = distributorRate.rate;
						entry.money = distributorRate.money;
						entry.limit = distributorRate.limit;
						entry.minimum = distributorRate.minimum;
						break;
					}
				}
			}
			for (DistributorCashRate distributorCashRate : cashList) {
				if (item.id.equals(distributorCashRate.distributor.id)) {
					item.cashRate = distributorCashRate.rate;
					item.cashMoney = distributorCashRate.money;
					item.cashLimit = distributorCashRate.limit;
					item.cashMinimum = distributorCashRate.minimum;
				}
			}
		});

		return new PageResult<>(page.getTotal(), rows);
	}

	public ExecuteResult<?> saveRate(DistributorRateMap param) {
		Distributor distributor = distributorRepository.findOne(param.id);
		if (distributor == null) {
			return ExecuteResult.fail("数据不存在");
		}
		repository.findAllByDistributorId(param.id).forEach(item -> repository.delete(item));
		if (CollectionUtils.isEmpty(param.rateList)) {
			return ExecuteResult.ok();
		}

		for (RateEntry item : param.rateList) {
			if (StringUtils.isBlank(PayTypeUtils.getName(item.type)))
				continue;
			DistributorRate entity = new DistributorRate();
			entity.type = item.type;
			entity.rate = item.rate;
			entity.money = item.money;
			entity.limit = item.limit;
			entity.minimum = item.minimum;
			entity.distributor = distributor;
			repository.save(entity);
		}
		if (String.valueOf(param.cashRate) != null || String.valueOf(param.cashMoney) != null
				|| String.valueOf(param.cashLimit) != null || String.valueOf(param.cashMinimum) != null) {
			DistributorCashRate cashEntity = new DistributorCashRate();
			cashEntity.rate = param.cashRate;
			cashEntity.money = param.cashMoney;
			cashEntity.limit = param.cashLimit;
			cashEntity.minimum = param.cashMinimum;
			cashEntity.distributor = distributor;
			cashRepository.save(cashEntity);
		}

		return ExecuteResult.ok();
	}

	public List<RateEntry> findAllRateEntry(String id) {
		List<DistributorRate> list = repository.findAllByDistributorId(id);
		List<RateEntry> rateEntries = new ArrayList<RateEntry>();

		for (int i = 0; i < list.size(); i++) {
			RateEntry rateEntry = new RateEntry();
			rateEntry.name = PayTypeUtils.getName(list.get(i).type);
			rateEntry.rate = list.get(i).rate;
			rateEntry.type = list.get(i).type;
			rateEntry.limit = list.get(i).limit;
			rateEntry.money = list.get(i).money;
			rateEntry.minimum = list.get(i).minimum;
			rateEntries.add(i, rateEntry);
		}
		return rateEntries;
	}

}
